package com.lxmes.sales.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.lxmes.base.domain.JcIteminfo;
import com.lxmes.base.domain.JcProductionLine;
import com.lxmes.base.domain.NcSupplier;
import com.lxmes.base.domain.NcTechDetail;
import com.lxmes.base.mapper.JcIteminfoMapper;
import com.lxmes.base.mapper.JcProductionLineMapper;
import com.lxmes.base.mapper.NcSupplierMapper;
import com.lxmes.base.mapper.NcTechDetailMapper;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.sales.domain.*;
import com.lxmes.sales.mapper.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.sales.service.INcSalesOrderService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.Validator;

/**
 * 销售正式订单Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-23
 */
@Service
public class NcSalesOrderServiceImpl implements INcSalesOrderService
{
    @Resource
    private NcSalesOrderMapper ncSalesOrderMapper;
    @Resource
    private NcOrderResourcesYsMapper ncOrderResourcesYsMapper;

    @Resource
    private NcProcessproPlanMapper ncProcessproPlanMapper;

    @Resource
    private NcSupplierMapper ncSupplierMapper;

    @Resource
    private JcIteminfoMapper jcIteminfoMapper;

    @Resource
    private JcProductionLineMapper jcProductionLineMapper;

    @Resource
    private NcProductPreSchedulingPlanMainMapper ncProductPreSchedulingPlanMainMapper;

    @Resource
    private NcTechDetailMapper ncTechDetailMapper;

    @Resource
    private NcContractMapper ncContractMapper;

    @Autowired
    protected Validator validator;



    /**
     * 查询销售正式订单
     *
     * @param id 销售正式订单主键
     * @return 销售正式订单
     */
    @Override
    public NcSalesOrder selectNcSalesOrderById(Long id)
    {
        NcSalesOrder ncSalesOrder = ncSalesOrderMapper.selectNcSalesOrderById(id);
        return ncSalesOrder;
    }

    /**
     * 查询销售正式订单列表
     *
     * @param ncSalesOrder 销售正式订单
     * @return 销售正式订单
     */
    @Override
    public List<NcSalesOrder> selectNcSalesOrderList(NcSalesOrder ncSalesOrder)
    {
        List<NcSalesOrder> ncSalesOrders = ncSalesOrderMapper.selectNcSalesOrderList(ncSalesOrder);
        return ncSalesOrders;
    }

    /**
     * 新增销售正式订单
     *
     * @param ncSalesOrder 销售正式订单
     * @return 结果
     */
    @Override
    public int insertNcSalesOrder(NcSalesOrder ncSalesOrder)
    {
        ncSalesOrder.setDeptId(SecurityUtils.getDeptId());
        return ncSalesOrderMapper.insertNcSalesOrder(ncSalesOrder);
    }

    /**
     * 修改销售正式订单
     *
     * @param ncSalesOrder 销售正式订单
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNcSalesOrder(NcSalesOrder ncSalesOrder)
    {

        ncSalesOrder.setUpdateBy(SecurityUtils.getUsername());
        ncSalesOrder.setUpdateTime(new Date());

        return ncSalesOrderMapper.updateNcSalesOrder(ncSalesOrder);
    }

    /**
     * 批量删除销售正式订单
     *
     * @param ids 需要删除的销售正式订单主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNcSalesOrderByIds(Long[] ids)
    {
        for(long id : ids){
            NcSalesOrder ncSalesOrder = ncSalesOrderMapper.selectNcSalesOrderById(id);
            if("0".equals(ncSalesOrder.getIsYsed())){
                ncSalesOrderMapper.deleteNcSalesOrderById(id);
                NcSalesOrder param = new NcSalesOrder();
                param.setContractNo(ncSalesOrder.getContractNo());
                List<NcSalesOrder> ncSalesOrders = ncSalesOrderMapper.selectNcSalesOrder(param);
                if(ncSalesOrders.size()==0){
                    //解合同封印
                    ncSalesOrderMapper.updateContractState("0",ncSalesOrder.getContractNo());
                }
            }else{
                throw new RuntimeException("该订单已经验算完成不能删除!");
            }
        }
        return 1;
    }

    /**
     * 删除销售正式订单信息
     *
     * @param id 销售正式订单主键
     * @return 结果
     */
    @Override
    public int deleteNcSalesOrderById(Long id)
    {
        return ncSalesOrderMapper.deleteNcSalesOrderById(id);
    }

    @Override
    @Transactional
    public int pushTozyys(Long[] ids) {
        for(long id : ids){
            NcSalesOrder ncSalesOrder = ncSalesOrderMapper.selectNcSalesOrderById(id);
            if("1".equals(ncSalesOrder.getCfbj())){
                JcIteminfo j=new JcIteminfo();
                j.setItemIdentifyCode(ncSalesOrder.getItemIdentifyCode());
                j.setClassId("C");
                JcIteminfo jcIteminfo = jcIteminfoMapper.selectJcIteminfoByIdentifyCode2(j);
                String lineSimpleCode = jcIteminfo.getLineSimpleCode();
                if(StringUtils.isNull(lineSimpleCode)){
                    throw new RuntimeException("请先维护物料的生产线简码！");
                }
                String[] simpleCode = lineSimpleCode.split(",");
                for(String code : simpleCode){
                    List<JcProductionLine> linelist= jcProductionLineMapper.selectJcProductionLineBySimoleCode(code);
                    if(linelist.size()==0){
                        throw new RuntimeException("请先维护产线的生产线简码！");
                    }
                    linelist = linelist.stream().filter(line->line.getLineName().contains("机加线")).collect(Collectors.toList());
                    for(JcProductionLine line : linelist){
                        //为剩余的做排产计划
                        NcProcessproPlan ncProcessproPlan = new NcProcessproPlan();
                        ncProcessproPlan.setFactoryCode("WR");
                        ncProcessproPlan.setFactoryName("玮瑞");
                        ncProcessproPlan.setOrderNo(ncSalesOrder.getOrderNumber());
                        ncProcessproPlan.setContractNo(ncSalesOrder.getContractNo());
                        /*TKPC20221128001*/
                        ncProcessproPlan.setPcCode("TKPC"+ NewNumber.createNewNumber(ncProcessproPlanMapper.getNewCode(DateUtils.getDate())));
                        ncProcessproPlan.setPcDate(new Date());
                        ncProcessproPlan.setJhDate(ncSalesOrder.getDeliveryDate());
                        ncProcessproPlan.setLineCode(line.getLineId());//根据物料识别号选取线体让他们选
                        ncProcessproPlan.setDyProCode(ncSalesOrder.getItemSubCode());
                        ncProcessproPlan.setProName(jcIteminfo.getItemName());
                        ncProcessproPlan.setOrderNum(ncSalesOrder.getNumber());//订单数量
                        if("2".equals(ncSalesOrder.getProdWay())){
                            ncProcessproPlan.setNoPcNum(ncSalesOrder.getZcNum().longValue());//未排产数量
                            ncProcessproPlan.setPcNum(ncSalesOrder.getZcNum().longValue());//排产数量
                        }else {
                            ncProcessproPlan.setNoPcNum(ncSalesOrder.getYuPcNum());//未排产数量
                            ncProcessproPlan.setPcNum(ncSalesOrder.getYuPcNum());//排产数量
                        }
                        ncProcessproPlan.setCreateBy(SecurityUtils.getUsername());
                        ncProcessproPlan.setCreateTime(new Date());
                        ncProcessproPlan.setItemIdentifyCode(ncSalesOrder.getItemIdentifyCode());
                        ncProcessproPlan.setContent(ncSalesOrder.getContent());
                        ncProcessproPlan.setDj(ncSalesOrder.getDj());
                        ncProcessproPlan.setSx(ncSalesOrder.getSx());
                        ncProcessproPlan.setMfmdj(ncSalesOrder.getMfmdj());
                        ncProcessproPlanMapper.insertNcProcessproPlan(ncProcessproPlan);
                        ncSalesOrder.setPcState("1");
                        ncSalesOrderMapper.updateNcSalesOrder(ncSalesOrder);
                    }
                }
            }else{
                //判断是不是外协
                String prodWay = ncSalesOrder.getProdWay();
                if("1".equals(prodWay)){
                    zcPart(ncSalesOrder.getNumber(),ncSalesOrder);
                }else{
                    //外协 走采购单
                    Integer wxNum = ncSalesOrder.getWxNum();
//                if(yuPcNum==wxNum.longValue()){
//                    wxPart(yuPcNum,ncSalesOrder);
//                }else{
                    //部分自产  部分采购
                    //自产部分  zcNum
                    Integer zcNum = ncSalesOrder.getZcNum();
                    zcPart(zcNum.longValue(),ncSalesOrder);
                    //外协部分  wxNum
                    wxPart(wxNum.longValue(),ncSalesOrder);
//                }
                }
                ncSalesOrder.setPcState("1");
                ncSalesOrderMapper.updateNcSalesOrder(ncSalesOrder);
            }
        }
        return 1;
    }

    @Override
    @Transactional
    public int splitSalesorder(NcSalesOrder ncSalesOrder) {
        int splitRow = ncSalesOrder.getSplitRow();
        NcSalesOrder ncSalesOrder1 = ncSalesOrderMapper.selectNcSalesOrderById(ncSalesOrder.getId());
        ncSalesOrder1.setCreateBy(SecurityUtils.getUsername());
        ncSalesOrder1.setCreateTime(ncSalesOrder1.getCreateTime());
        ncSalesOrder1.setUpdateTime(new Date());
        ncSalesOrder1.setUpdateBy(SecurityUtils.getUsername());
        ncSalesOrder1.setCfbj("1");
        for(int i=0;i<splitRow-1;i++){
            ncSalesOrderMapper.insertNcSalesOrder(ncSalesOrder1);
        }
        return 1;
    }

    public void zcPart(Long zcNum,NcSalesOrder ncSalesOrder){

        //自产
        //走资源验算
        NcOrderResourcesYs ncOrderResourcesYs = new NcOrderResourcesYs();
        BeanUtils.copyProperties(ncSalesOrder,ncOrderResourcesYs);
        ncOrderResourcesYs.setHth(ncSalesOrder.getContractNo());
        ncOrderResourcesYs.setOrderNo(ncSalesOrder.getOrderNumber());
        ncOrderResourcesYs.setIfOrderUndo(ncSalesOrder.getOrderRevocFlag());
        ncOrderResourcesYs.setOrderNumber(ncSalesOrder.getNumber());
        ncOrderResourcesYs.setCreateBy(SecurityUtils.getUsername());
        ncOrderResourcesYs.setYuPcNum(zcNum);
        ncOrderResourcesYs.setItemIdentifyCode(ncSalesOrder.getItemIdentifyCode());

        ncOrderResourcesYs.setCreateTime(new Date());
        ncOrderResourcesYs.setItemId(ncSalesOrder.getItemSubCode());
        ncOrderResourcesYs.setItemName(ncSalesOrder.getItemName());
        String itemSubCode = ncSalesOrder.getItemSubCode();
        //检索该物料并且可用数量大于0的
        List<CcStockInfoVo> stockInfos = ncOrderResourcesYsMapper.getStockInfoBySubCode(itemSubCode);
        //多个库位
        if(stockInfos.size()!=0){
            //库存有呆滞并且大于0
//            List<CcStockinfoInfoVo> dzInfos = stockInfos.stream().filter(ccStockinfoInfoVo -> ccStockinfoInfoVo.getIsDaizhi().equals("1")).sorted(Comparator.comparing(CcStockinfoInfoVo::getLot)).collect(Collectors.toList());
            BigDecimal kyTotal = stockInfos.stream().map(CcStockInfoVo::getKyNum).reduce((a, b) -> (a.add(b))).get();
            //预排数量
            Long number = zcNum;
            //呆滞数量总和大于等于预排数量
            if(kyTotal.longValue()>=number){
                //就不用排产了直接锁账
                Long  subNum = number;
                for(CcStockInfoVo stock : stockInfos){
                    BigDecimal kyNum = stock.getKyNum();
                    if(kyNum.longValue()>=subNum){
                        //更新锁账
                        stock.setLockNum(stock.getLockNum().add(new BigDecimal(subNum)));
                        stock.setKyNum(stock.getKyNum().subtract(new BigDecimal(subNum)));
                        int i = ncOrderResourcesYsMapper.updateStockLockNum(stock);
                        if(i==0){
                            throw new RuntimeException("锁账更新失败,请重试！");
                        }
                        ncOrderResourcesYs.setKcNum(kyTotal.longValue());
                        ncOrderResourcesYs.setPcNum(0L);
                        ncOrderResourcesYs.setKwbm(stock.getKuweiCode());
                        ncOrderResourcesYs.setKwNum(stock.getKcNum().longValue());
                        ncOrderResourcesYsMapper.insertNcOrderResourcesYs(ncOrderResourcesYs);
                        break;
                    }else{
                        stock.setLockNum(stock.getLockNum().add(kyNum));
                        stock.setKyNum(stock.getKyNum().subtract(kyNum));
                        int i = ncOrderResourcesYsMapper.updateStockLockNum(stock);
                        subNum = subNum-kyNum.longValue();
                        if(i==0){
                            throw new RuntimeException("锁账更新失败,请重试！");
                        }
                        ncOrderResourcesYs.setKcNum(kyTotal.longValue());
                        ncOrderResourcesYs.setPcNum(subNum);
                        ncOrderResourcesYs.setKwbm(stock.getKuweiCode());
                        ncOrderResourcesYs.setKwNum(stock.getKcNum().longValue());
                        ncOrderResourcesYsMapper.insertNcOrderResourcesYs(ncOrderResourcesYs);
                    }
                }
            }else{
                long subNum = number - kyTotal.longValue();
                long subTotal = number;
                //呆滞数量总和小于预排数量
                for(CcStockInfoVo stock : stockInfos){
                    //修改每一条库存数量
                    BigDecimal kyNum = stock.getKyNum();
                    stock.setLockNum(stock.getLockNum().add(kyNum));
                    stock.setKyNum(stock.getKyNum().subtract(kyNum));
                    int i = ncOrderResourcesYsMapper.updateStockLockNum(stock);
                    subTotal = subTotal-kyNum.longValue();
                    if(i==0){
                        throw new RuntimeException("锁账更新失败,请重试！");
                    }

                    //添加每一条的资源验算
                    ncOrderResourcesYs.setKcNum(kyTotal.longValue());
                    ncOrderResourcesYs.setPcNum(subTotal);
                    ncOrderResourcesYs.setKwbm(stock.getKuweiCode());
                    ncOrderResourcesYs.setKwNum(stock.getKcNum().longValue());
                    ncOrderResourcesYsMapper.insertNcOrderResourcesYs(ncOrderResourcesYs);
                    JcIteminfo j=new JcIteminfo();
                    j.setItemIdentifyCode(ncSalesOrder.getItemIdentifyCode());
                    j.setClassId("C");
                    JcIteminfo jcIteminfo = jcIteminfoMapper.selectJcIteminfoByIdentifyCode2(j);
                    String lineSimpleCode = jcIteminfo.getLineSimpleCode();
                    if(StringUtils.isNull(lineSimpleCode)){
                        throw new RuntimeException("该物料识别码【"+ncSalesOrder.getItemIdentifyCode()+"】未维护生产线简码,请先维护！");
                    }
                    String[] simpleCode = lineSimpleCode.split(",");
                    for(String code : simpleCode){
                        List<JcProductionLine> linelist= jcProductionLineMapper.selectJcProductionLineBySimoleCode(code);
                        if(linelist.size()==0){
                            throw new RuntimeException("该简码【"+code+"】未查到相关生成信息，请查看生产线页面与之相匹配！");
                        }
                        for(JcProductionLine line : linelist){
                            //为剩余的做排产计划
                            NcProcessproPlan ncProcessproPlan = new NcProcessproPlan();
                            ncProcessproPlan.setFactoryCode("WR");
                            ncProcessproPlan.setFactoryName("玮瑞");
                            ncProcessproPlan.setOrderNo(ncSalesOrder.getOrderNumber());
                            ncProcessproPlan.setContractNo(ncSalesOrder.getContractNo());
                            /*TKPC20221128001*/
                            ncProcessproPlan.setPcCode("TKPC"+ NewNumber.createNewNumber(ncProcessproPlanMapper.getNewCode(DateUtils.getDate())));
                            ncProcessproPlan.setPcDate(new Date());
                            ncProcessproPlan.setJhDate(ncSalesOrder.getDeliveryDate());
                            ncProcessproPlan.setLineCode(line.getLineId());//根据物料识别号选取线体让他们选
                            ncProcessproPlan.setDyProCode(itemSubCode);
                            ncProcessproPlan.setProName(jcIteminfo.getItemName());
                            ncProcessproPlan.setOrderNum(ncSalesOrder.getNumber());//订单数量
//                        ncProcessproPlan.setPcNum(subNum);//排产数量
//                        ncProcessproPlan.setNoPcNum(subNum);//排产数量
                            String type = ncSalesOrder.getOutsourType();
                            if("K".equals(type)){
                                if(line.getLineName().contains("下料线")){
                                    ncProcessproPlan.setWxNum(ncSalesOrder.getWxNum());
                                    ncProcessproPlan.setOutsourType("K");
                                    ncProcessproPlan.setNoPcNum(subNum);//未排产数量
                                    ncProcessproPlan.setPcNum(subNum);//排产数量
                                }
                            }else if("P".equals(type)){
                                if(line.getLineName().contains("锻造线")){
                                    ncProcessproPlan.setWxNum(ncSalesOrder.getWxNum());//外协数量
                                    ncProcessproPlan.setOutsourType("P");//外协类型
                                    ncProcessproPlan.setNoPcNum(subNum);//未排产数量
                                    ncProcessproPlan.setPcNum(subNum);//排产数量
                                }
                            }else if("C".equals(type)){
                                ncProcessproPlan.setWxNum(ncSalesOrder.getWxNum());//外协数量
                                ncProcessproPlan.setOutsourType(ncSalesOrder.getOutsourType());//外协类型
                                ncProcessproPlan.setNoPcNum(subNum);//未排产数量
                                ncProcessproPlan.setPcNum(subNum);//排产数量
                            }else{
                                ncProcessproPlan.setNoPcNum(ncSalesOrder.getYuPcNum());//未排产数量
                                ncProcessproPlan.setPcNum(ncSalesOrder.getYuPcNum());//排产数量
                            }
                            ncProcessproPlan.setCreateBy(SecurityUtils.getUsername());
                            ncProcessproPlan.setCreateTime(new Date());
                            ncProcessproPlan.setItemIdentifyCode(ncSalesOrder.getItemIdentifyCode());
                            ncProcessproPlan.setContent(ncSalesOrder.getContent());
                            if(line.getLineName().contains("机加线")){
                                ncProcessproPlan.setDj(ncSalesOrder.getDj());
                                ncProcessproPlan.setSx(ncSalesOrder.getSx());
                                ncProcessproPlan.setMfmdj(ncSalesOrder.getMfmdj());
                            }
                            ncProcessproPlanMapper.insertNcProcessproPlan(ncProcessproPlan);
                        }
                    }
                }
            }
        }else{
            //库存没有呆滞  自产全部走排产
            Long orderNum = zcNum;
            ncOrderResourcesYs.setKcNum(0L);
            ncOrderResourcesYs.setPcNum(orderNum);
            ncOrderResourcesYs.setKwNum(0L);
            ncOrderResourcesYsMapper.insertNcOrderResourcesYs(ncOrderResourcesYs);
            JcIteminfo j=new JcIteminfo();
            j.setItemIdentifyCode(ncSalesOrder.getItemIdentifyCode());
            j.setClassId("C");
            JcIteminfo jcIteminfo = jcIteminfoMapper.selectJcIteminfoByIdentifyCode2(j);
            if(jcIteminfo==null){
                throw new RuntimeException("该物料识别号【"+ncSalesOrder.getItemIdentifyCode()+"】未查到成品物料信息,请先维护此物料！");
            }
            String lineSimpleCode = jcIteminfo.getLineSimpleCode();
            if(StringUtils.isNull(lineSimpleCode)){
                throw new RuntimeException("该物料识别码【"+ncSalesOrder.getItemIdentifyCode()+"】未维护生产线简码,请先维护！");
            }
            String[] simpleCode = lineSimpleCode.split(",");
            for(String code : simpleCode){
                List<JcProductionLine> linelist= jcProductionLineMapper.selectJcProductionLineBySimoleCode(code);
                if(linelist.size()==0){
                    throw new RuntimeException("该简码【"+code+"】未查到相关生成信息，请查看生产线页面与之相匹配！");
                }
                for(JcProductionLine line : linelist){
                    NcProcessproPlan ncProcessproPlan = new NcProcessproPlan();
                    ncProcessproPlan.setFactoryCode("WR");
                    ncProcessproPlan.setFactoryName("玮瑞");
                    ncProcessproPlan.setOrderNo(ncSalesOrder.getOrderNumber());
                    ncProcessproPlan.setContractNo(ncSalesOrder.getContractNo());
                    /*TKPC20221128001*/
                    ncProcessproPlan.setPcCode("TKPC"+ NewNumber.createNewNumber(ncProcessproPlanMapper.getNewCode(DateUtils.getDate())));
                    ncProcessproPlan.setPcDate(new Date());
                    ncProcessproPlan.setJhDate(ncSalesOrder.getDeliveryDate());
                    ncProcessproPlan.setLineCode(line.getLineId());//根据物料识别号选取线体让他们选
//                ncProcessproPlan.setLineName(line.getLineName());//根据物料识别号选取线体让他们选
                    ncProcessproPlan.setDyProCode(itemSubCode);
                    ncProcessproPlan.setProName(jcIteminfo.getItemName());
                    ncProcessproPlan.setOrderNum(ncSalesOrder.getNumber());//订单数量

                    ncProcessproPlan.setCreateBy(SecurityUtils.getUsername());
                    ncProcessproPlan.setCreateTime(new Date());
                    ncProcessproPlan.setItemIdentifyCode(ncSalesOrder.getItemIdentifyCode());
                    String type = ncSalesOrder.getOutsourType();
                    if("K".equals(type)){
                        if(line.getLineName().contains("下料线")){
                            ncProcessproPlan.setWxNum(ncSalesOrder.getWxNum());//外协数量
                            ncProcessproPlan.setOutsourType("K");//外协类型
                            ncProcessproPlan.setNoPcNum(orderNum);//未排产数量
                            ncProcessproPlan.setPcNum(orderNum);//排产数量
                        }
                    }else if("P".equals(type)){
                        if(line.getLineName().contains("锻造线")){
                            ncProcessproPlan.setWxNum(ncSalesOrder.getWxNum());//外协数量
                            ncProcessproPlan.setOutsourType("P");//外协类型
                            ncProcessproPlan.setNoPcNum(orderNum);//未排产数量
                            ncProcessproPlan.setPcNum(orderNum);//排产数量
                        }
                    }else if("C".equals(type)){
                        ncProcessproPlan.setWxNum(ncSalesOrder.getWxNum());//外协数量
                        ncProcessproPlan.setOutsourType(ncSalesOrder.getOutsourType());//外协类型
                        ncProcessproPlan.setNoPcNum(orderNum);//未排产数量
                        ncProcessproPlan.setPcNum(orderNum);//排产数量
                    }else{
                        ncProcessproPlan.setNoPcNum(ncSalesOrder.getYuPcNum());//未排产数量
                        ncProcessproPlan.setPcNum(ncSalesOrder.getYuPcNum());//排产数量
                    }
                    ncProcessproPlan.setContent(ncSalesOrder.getContent());
                    if(line.getLineName().contains("机加线")){
                        ncProcessproPlan.setDj(ncSalesOrder.getDj());
                        ncProcessproPlan.setSx(ncSalesOrder.getSx());
                        ncProcessproPlan.setMfmdj(ncSalesOrder.getMfmdj());
                    }
                    ncProcessproPlanMapper.insertNcProcessproPlan(ncProcessproPlan);
                }
            }
        }
    }
    public void wxPart(Long wxNum,NcSalesOrder ncSalesOrder){
        CcPurchaseInfoVo ccPurchaseInfoVo = new CcPurchaseInfoVo();
        ccPurchaseInfoVo.setFactoryName("玮瑞");
        ccPurchaseInfoVo.setFactoryCode(SecurityUtils.getFactoryCode());
        String dh = "CG-"+ NewNumber.createNewNumber(ncSalesOrderMapper.getCaiGouNewCode(DateUtils.getDate()));
        ccPurchaseInfoVo.setDanhao(dh);
        ccPurchaseInfoVo.setCreateTime(DateUtils.getNowDate());
        ccPurchaseInfoVo.setCreateBy(SecurityUtils.getUsername());
        ccPurchaseInfoVo.setYhType("订单外协采购计划");
        ccPurchaseInfoVo.setYhDanhao(dh);
        ccPurchaseInfoVo.setAsnDanhao(dh);
        ccPurchaseInfoVo.setItemCode(ncSalesOrder.getItemSubCode());
        ccPurchaseInfoVo.setItemId(ncSalesOrder.getItemSubCode());
        ccPurchaseInfoVo.setItemName(ncSalesOrder.getItemName());
        ccPurchaseInfoVo.setSpec(ncSalesOrder.getProductModel());
        NcSupplier supplier = ncSupplierMapper.selectNcSupplierByCode(ncSalesOrder.getSupplierCode());
        ccPurchaseInfoVo.setSupplyId(supplier.getId());
        ccPurchaseInfoVo.setSupplyName(supplier.getSupplierName());
        ccPurchaseInfoVo.setContractCode(ncSalesOrder.getContractNo());
        ccPurchaseInfoVo.setClassId(ncSalesOrder.getOutsourType());
        if("K".equals(ncSalesOrder.getOutsourType())){
            ccPurchaseInfoVo.setClassName("料块");
        }else if("P".equals(ncSalesOrder.getOutsourType())){
            ccPurchaseInfoVo.setClassName("毛坯");
        }else if("B".equals(ncSalesOrder.getOutsourType())){
            ccPurchaseInfoVo.setClassName("半成品");
        }else{
            ccPurchaseInfoVo.setClassName("成品");
        }

        ccPurchaseInfoVo.setYhNum(new BigDecimal(wxNum));
        ccPurchaseInfoVo.setJhUnitPrice(ncSalesOrder.getItemPrice());
        ncSalesOrderMapper.insertCcPurchaseInfo(ccPurchaseInfoVo);
    }

    @Override
    public int pushToPczb(Long[] ids) {
        for(long id : ids){
            NcSalesOrder ncSalesOrder = ncSalesOrderMapper.selectNcSalesOrderById(id);
            if("1".equals(ncSalesOrder.getPcState())){
                throw new RuntimeException("该产品"+ncSalesOrder.getItemName()+"已经审核排产！");
            }
            NcProductPreSchedulingPlanMain ncProductPreSchedulingPlanMain = new NcProductPreSchedulingPlanMain();
            ncProductPreSchedulingPlanMain.setFactoryCode(ncSalesOrder.getFactoryCode());
            ncProductPreSchedulingPlanMain.setFactoryName(ncSalesOrder.getFactoryName());
            ncProductPreSchedulingPlanMain.setContract(ncSalesOrder.getContractNo());
            ncProductPreSchedulingPlanMain.setOrderNo(ncSalesOrder.getOrderNumber());
            ncProductPreSchedulingPlanMain.setOrderType(ncSalesOrder.getOrderType());
            ncProductPreSchedulingPlanMain.setJhDate(ncSalesOrder.getDeliveryDate());
//            ncProductPreSchedulingPlanMain.setProType(ncSalesOrder.getProductType());
            ncProductPreSchedulingPlanMain.setProductNumber(ncSalesOrder.getItemCode());
            ncProductPreSchedulingPlanMain.setProductName(ncSalesOrder.getItemName());
            ncProductPreSchedulingPlanMain.setPreProductionNumber("YP"+ NewNumber.createNewNumber(ncProductPreSchedulingPlanMainMapper.getPcMainCode(DateUtils.getDate())));
            ncProductPreSchedulingPlanMain.setPreProductionDate(new Date());
            ncProductPreSchedulingPlanMain.setOrderNum(ncSalesOrder.getNumber());
            NcTechDetail ncTechDetail = new NcTechDetail();
            ncTechDetail.setCpbm(ncSalesOrder.getItemCode());
            List<NcTechDetail> ncTechDetails = ncTechDetailMapper.selectNcTechDetailList(ncTechDetail);
            if(ncTechDetails.size()>0){
                ncProductPreSchedulingPlanMain.setCjCode(ncTechDetails.get(0).getCjCode());
                ncProductPreSchedulingPlanMain.setWorkshopName(ncTechDetails.get(0).getCjName());
                ncProductPreSchedulingPlanMain.setLineId(ncTechDetails.get(0).getLineCode());
                ncProductPreSchedulingPlanMain.setLineName(ncTechDetails.get(0).getLineName());
            }
            ncProductPreSchedulingPlanMain.setCreateBy(SecurityUtils.getUsername());
            ncProductPreSchedulingPlanMain.setCreateTime(new Date());
            ncProductPreSchedulingPlanMainMapper.insertNcProductPreSchedulingPlanMain(ncProductPreSchedulingPlanMain);
            NcSalesOrder param= new NcSalesOrder();
            param.setPcState("1");
            param.setId(id);
            ncSalesOrderMapper.updateNcSalesOrder(param);
        }
        return 1;
    }

    @Override
    public String imporIteminfo(List<NcSalesOrder> orderList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(orderList) || orderList.size() == 0)
        {
            throw new ServiceException("导入物料数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        String newNumber = NewNumber.createNewNumber(ncContractMapper.getNewCode(DateUtils.getDate()));
        for (NcSalesOrder order : orderList)
        {
            try
            {
                BeanValidators.validateWithException(validator, order);
                order.setCreateBy(operName);
                order.setCreateTime(new Date());
                order.setOrderNumber("XSDD"+ newNumber);
                this.insertNcSalesOrder(order);
                successNum++;
                successMsg.append("<br/>" + successNum + "、数据 " + order.getOrderNumber()+ " 导入成功");
               /* if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, iteminfo);
                    iteminfo.setCreateBy(operName);
                    iteminfo.setUpdateBy(operName);
                    iteminfo.setTenantId(SecurityUtils.getDeptId()+"");
                    this.insertJcItembzinfo(iteminfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、标准 " + iteminfo.getItemBzName() + " 导入成功");
                }
                else
                {
                    BeanValidators.validateWithException(validator, iteminfo);
                    iteminfo.setUpdateBy(operName);
                    this.updateJcItembzinfo(iteminfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + iteminfo.getItemBzName()+ " 更新成功");
                }*/
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + order.getOrderNumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
